/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.activequant.container.report.statistics;

import java.util.Hashtable;

import org.activequant.core.domainmodel.account.OrderBook;
import org.activequant.core.domainmodel.account.OrderHistory;

/**
 * An implementation of the IStatisticsComputer interface. Calculates the average order size in an OrderBook.
 * @author Ulrich Staudinger
 */
public class AverageOrderSize implements IStatisticsComputer {
	/**
	 * if the given anObject(Object) is an OrderBook, this method will calculate the average order size of its closed OrderHistory elements. Then it maps the average order size
	 * under KEY("AVGORDERSIZE") into the given aStack(Hashtable&lt;String, Object&gt;)<br/>
	 * <strong>avg. order size</strong> is calculated as: total quantity / total number of orders
	 */
    public void compute(Hashtable<String, Object> aStack, Object anObject)
    {
        int myOrderCount = 0;
        double myTotalQuantity = 0;
        if(anObject instanceof OrderBook)
        {
            OrderBook myOrderBook = (OrderBook) anObject;
            OrderHistory[] myHistories = myOrderBook.getClosedHistories();
            for(OrderHistory myHistory : myHistories)
            {
                myTotalQuantity += myHistory.getOrder().getQuantity();
                myOrderCount ++; 
            }
        }
        if(myOrderCount!=0)
        {
            aStack.put(KEY, (myTotalQuantity/myOrderCount));
        }
        else
            aStack.put(KEY, 0);

    }
    /**
     * returns "Avg. Order Size"
     */
    public String getStatisticsName()
    {
        return "Avg. Order Size";
    }
    /**
     * public static final String KEY = "AVGORDERSIZE";
     */
    public static final String KEY = "AVGORDERSIZE";

}
